package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.xxx.xbutler.data.RoomMapper;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.Room;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 获取群二维码回调
 *
 * <pre>
 * {
 *     "nType": 4008,
 *     "vcMerchantNo": "",
 *     "vcRobotWxId": "",
 *     "vcRobotSerialNo": "",
 *     "vcSerialNo": "",
 *     "nResult": 1,
 *     "vcResult": "SUCCESS",
 *     "Data": {
 *         "vcChatRoomId": "",
 *         "vcChatRoomSerialNo": "",
 *         "vcChatRoomQRCode": "",
 *         "vcShortQrCodeLink": ""
 *     }
 * }
 * </pre>
 */
@Slf4j
@Component
public class _4008Handler implements CallbackHandler {

    @Resource
    private RoomMapper roomMapper;

    @Override
    public String name() {
        return "获取群二维码回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 4008;
    }

    @Override
    public void handle(Callback callback) {
        JSONObject data = (JSONObject) callback.getData();
        LambdaUpdateWrapper<Room> wrapper = new LambdaUpdateWrapper<Room>()
                .set(Room::getQrcode, data.getString("vcChatRoomQRCode"))
                .set(Room::getShortQrcode, data.getString("vcShortQrCodeLink"))
                .eq(Room::getId, data.getString("vcChatRoomId"));
        roomMapper.update(wrapper);
    }
}
